import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useCallback, useMemo } from 'react';
import { DeptRatioManageAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

const RuleManageServices = (curRes: Record<string, any>) => {
  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: DeptRatioManageAPI.IAssetTableRequestType) => {
      let reqUrl = `${baseUrl}/logController/dataGrid`;

      let PostParams = {
        current: 1,
        pageSize: 10,
        sort: 'operTime',
        order: 'desc',
        ...reqParams,
      };

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: DeptRatioManageAPI.IAssetTableRequestType | any) => {
    /** 操作时间处理 */
    if (reqParams?.operTime) {
      const [operTimeStart, operTimeEnd] = reqParams.operTime;
      reqParams['operTimeStart'] = operTimeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['operTimeEnd'] = operTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['operTime'];
    }
    run({ ...reqParams });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      searchList: ManageSearchColumns(),
    };
  }, [curRes]);

  return {
    loading,
    reloadRequest,
    ...tableValues /**页面列表值*/,
    ...searchList /**搜索列表配置项*/,
  };
};

export { RuleManageServices };
